package bb.lanxing.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import bb.lanxing.R;
import bb.lanxing.model.LevelDataItem;
import bb.lanxing.view.AreaPicker;

import java.util.List;

public class AreaSelectionDialog extends Dialog {
    private AreaPicker cityPicker;
    private AreaPicker districtPicker;
    private OnAreaSelectionListener onAreaSelectionListener;
    private AreaPicker provincePicker;

    public interface OnAreaSelectionListener {
        void onArea(String province, String city, String district);
    }

    public AreaSelectionDialog(Context context) {
        super(context, R.style.SprintItemSelector);
        initViews();
    }

    private void initViews() {
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        attributes.gravity = Gravity.BOTTOM;
        attributes.width = -1;
        attributes.height = -2;
        setContentView(R.layout.dialog_area_selector);
        this.provincePicker = (AreaPicker) findViewById(R.id.np_province);
        this.cityPicker = (AreaPicker) findViewById(R.id.np_city);
        this.districtPicker = (AreaPicker) findViewById(R.id.np_district);
        ((TextView) findViewById(R.id.tv_cancel)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AreaSelectionDialog.this.dismiss();
            }
        });
        ((TextView) findViewById(R.id.tv_confirm)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AreaSelectionDialog.this.preformConfirm();
                AreaSelectionDialog.this.dismiss();
            }
        });
    }

    public void preformConfirm() {
        if (this.onAreaSelectionListener == null) {
            return;
        }
        LevelDataItem area = this.provincePicker.getArea();
        LevelDataItem area2 = this.cityPicker.getArea();
        LevelDataItem area3 = this.districtPicker.getArea();
        this.onAreaSelectionListener.onArea(area != null ? area.getTitle() : null, area2 != null ? area2.getTitle() : null, area3 != null ? area3.getTitle() : null);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        this.onAreaSelectionListener = null;
    }

    @Override
    public void cancel() {
        super.cancel();
        this.onAreaSelectionListener = null;
    }

    public static AreaSelectionDialog show(Context context, List<LevelDataItem> dataItemList, String province, String city, String district) {
        AreaSelectionDialog areaSelectionDialog = new AreaSelectionDialog(context);
        areaSelectionDialog.districtPicker.initialArea(district).parent(areaSelectionDialog.cityPicker);
        areaSelectionDialog.cityPicker.initialArea(city).parent(areaSelectionDialog.provincePicker);
        areaSelectionDialog.provincePicker.initialArea(province).init(dataItemList);
        areaSelectionDialog.show();
        return areaSelectionDialog;
    }

    public void setOnAreaSelectionListener(OnAreaSelectionListener onAreaSelectionListener) {
        this.onAreaSelectionListener = onAreaSelectionListener;
    }
}
